package cn.sgy.infrastructure.file.util;

import cn.sgy.infrastructure.file.enums.DataTypeEnum;
import lombok.extern.slf4j.Slf4j;

import java.time.format.DateTimeFormatter;

/**
 * @author sgy
 * @description 根据类型识别工具
 * @date 2023-03-09
 */
@Slf4j
public class FileDataTypeUtil {
    final static DateTimeFormatter DTF = DateTimeFormatter.ofPattern("yyyy/MM");
    private final static String IMAGE = "image";
    private final static String VIDEO = "video";
    private final static String DIR = "application/x-director";
    private final static String AUDIO = "audio";
    private final static String TEXT = "text";

    /**
     * 根据mine类型，返回文件类型
     */
    public static DataTypeEnum getDataType(String mime) {
        if (mime == null || "".equals(mime)) {
            return DataTypeEnum.OTHER;
        }
        if (mime.contains(IMAGE)) {
            return DataTypeEnum.IMAGE;
        } else if (mime.contains(TEXT)
                || mime.startsWith("application/vnd.ms-excel")
                || mime.startsWith("application/msword")
                || mime.startsWith("application/pdf")
                || mime.startsWith("application/vnd.ms-project")
                || mime.startsWith("application/vnd.ms-works")
                || mime.startsWith("application/x-javascript")
                || mime.startsWith("application/vnd.openxmlformats-officedocument")
                || mime.startsWith("application/vnd.ms-word.document.macroEnabled")
                || mime.startsWith("application/vnd.ms-word.template.macroEnabled")
                || mime.startsWith("application/vnd.ms-powerpoint")
        ) {
            return DataTypeEnum.DOC;
        } else if (mime.contains(VIDEO)) {
            return DataTypeEnum.VIDEO;
        } else if (mime.contains(DIR)) {
            return DataTypeEnum.DIR;
        } else if (mime.contains(AUDIO)) {
            return DataTypeEnum.AUDIO;
        } else {
            return DataTypeEnum.OTHER;
        }
    }

//    public static String getUploadPathPrefix(String uploadPathPrefix) {
//        //日期文件夹
//        String secDir = LocalDate.now().format(DTF);
//        // web服务器存放的绝对路径 D:\\uploadFiles\\2020\\05
//        String absolutePath = Paths.get(uploadPathPrefix, secDir).toString();
//        return absolutePath;
//    }
//
//    public static String getRelativePath(String pathPrefix, String path) {
//        String remove = StrUtil.remove(path, pathPrefix + File.separator);
//
//        log.info("remove={}, index={}", remove, remove.lastIndexOf(File.separator));
//        String relativePath = StringUtils.substring(remove, 0, remove.lastIndexOf(File.separator));
//        return relativePath;
//    }

}
